package redisson.rest;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;

import org.redisson.api.GeoEntry;
import org.redisson.api.GeoUnit;
import org.redisson.api.RGeo;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import lombok.extern.slf4j.Slf4j;

@Slf4j
//@RestController
@RequestMapping("/geo")
public class GeoController {

	@Autowired
	RedissonClient redissonClient;

	@PostConstruct
	public void init() {
		RGeo<String> geo = redissonClient.getGeo("redis:geo");
		List<GeoEntry> list = new ArrayList<>();
		list.add(new GeoEntry(121.473701D, 31.230416D, "shop1"));// 人民广场
		list.add(new GeoEntry(121.505469D, 31.235372D, "shop2"));// 陆家嘴
		list.add(new GeoEntry(121.417801D, 31.199944D, "shop3"));// 虹桥
		long l = geo.add(list.toArray(new GeoEntry[list.size()]));

	}

	/** 计算 shop1-shop2 距离 */
	@GetMapping("/distance")
	public Double distance() {
		RGeo<String> geo = redissonClient.getGeo("redis:geo");
		return geo.dist("shop1", "shop3", GeoUnit.KILOMETERS);
	}
}
